package dz.chuxin.app.core;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import dz.chuxin.app.R;
import dz.chuxin.framework.widget.recycler.BaseViewHolder;
import dz.chuxin.framework.widget.recycler.SectionModel;

/**
 * 支持分组的 RecyclerView Activity 基类
 * Created by chuxin on 2016/7/1.
 */
public abstract class BaseSectionRecyclerActivity<T> extends BaseRecyclerActivity<SectionModel<T>> {

    protected static final int VIEW_TYPE_SECTION_HEADER = 1;
    protected static final int VIEW_TYPE_SECTION_CONTENT = 2;

    @Override
    protected BaseViewHolder getRecyclerViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_SECTION_HEADER) {
            return onCreateSectionHeaderViewHolder(parent);
        }
        return onCreateSectionViewHolder(parent, viewType);
    }

    protected abstract BaseViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType);

    protected BaseViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent) {
        return new SectionHeaderViewHolder(
                getLayoutInflater().inflate(R.layout.widget_recycler_section_header, parent, false));
    }

    @Override
    protected int getViewType(int position) {
        return mDataList.get(position).isHeader ? VIEW_TYPE_SECTION_HEADER : VIEW_TYPE_SECTION_CONTENT;
    }

    @Override
    protected boolean isItemSectionHeader(int position) {
        return mDataList.get(position).isHeader;
    }

    // ----------------------------------------------------
    class SectionHeaderViewHolder extends BaseViewHolder {

        TextView mHeaderText;

        public SectionHeaderViewHolder(View itemView) {
            super(itemView);
            mHeaderText = (TextView) itemView.findViewById(R.id.header_text);
        }

        @Override
        protected void onBindViewHolder(int position) {
            mHeaderText.setText(mDataList.get(position).header);
        }
    }
}
